
import UIKit

class TalkfunAlertNewView: UIView {
  
   
    @objc public enum TalkfunAlertType: Int {
        /**确定要提交吗？*/
       case submit
        /**你还有题目未做完,确定要提交吗?*/
       case notCompletedButWantToSubmit
        /**该测试已被删除*/
       case deleted
        /**讲师已结束作答                */
       case endAnswerAndTheStudentHasSubmitted
        /**答题时间到                         */
       case endOfExamTimeAndTheStudentHasSubmitted
        /**另一个测试讲师已结束作答   */
       case otherTestQuestionsEndedEarlyAndTheStudentHasSubmitted
        /**另一个测试答题时间到      */
       case otherTestQuestionsEndedAndTheStudentHasSubmitted
        /**讲师已结束作答 ,                    (试题已自动提交)*/
       case endAnswer
        /**答题时间到                              (试题已自动提交)*/
       case endOfExamTime
        /**另一个测试讲师已结束作答     (试题已自动提交)*/
       case otherTestQuestionsEndedEarly
        /**另一个测试答题时间到           (试题已自动提交)*/
       case otherTestQuestionsEnded
        /**该表单已被删除
          *讲师端删除问卷，学员端已在当前被删除的作答页时，弹出提示：该表单已被删除，无法继续做答，点击【知道了】关闭做答页
         */
        case questionnaireDeleted
        /**讲师回收问卷
          *学员正在填写表单，讲师回收表单，弹出弹窗，点击【查看详情】，跳转到问卷列表页
         */
        case endTheQuestionnaireEarly
        /**当前有未填写的题目，请填写完整！
          *必答题，未填写，点提交，弹出提示
         */
        case missingRequiredQuestions
        
        /**确定要提交吗？  表单专用*/
       case submitToFill
    }
    
    let cancelButton = UIButton()
    let sureButton = UIButton()
    
    let tileOne = UILabel()
    let tileTwo = UILabel()
    /**查看详情**/
    let seeDetails = UIButton()
    
    var type:TalkfunAlertType = TalkfunAlertType.submit
    //关闭
    var close:TalkfunIncreaseButton = TalkfunIncreaseButton()
    override init(frame: CGRect) {
        super.init(frame: frame)
          //阴影
          self.layer.shadowOffset = CGSize(width: 1, height: 1)
          self.layer.shadowOpacity = 0.3
          self.layer.shadowColor = UIColor.lightGray.cgColor
        
        self.layer.cornerRadius = 6
        self.backgroundColor = UIColor(red: 1, green: 1, blue: 1, alpha: 1)
        
        
        self.addSubview(self.cancelButton)
        self.cancelButton.titleLabel?.font = UIFont(name: "PingFang SC", size: 17)
        self.cancelButton.backgroundColor = UIColor(red: 0.96, green: 0.96, blue: 0.98, alpha: 1)
        self.cancelButton.layer.cornerRadius = 4
        self.cancelButton.setTitle("取消", for: UIControl.State.normal)
        self.cancelButton.setTitleColor(UIColor(red: 0.2, green: 0.2, blue: 0.2,alpha:1), for: UIControl.State.normal)

        
        self.addSubview(self.sureButton)
        self.sureButton.titleLabel?.font = UIFont(name: "PingFang SC", size: 17)
        self.sureButton.backgroundColor = UIColor(red: 0.25, green: 0.59, blue: 0.92, alpha: 1)
        self.sureButton.layer.cornerRadius = 4
        self.sureButton.setTitle("确定", for: UIControl.State.normal)
        self.sureButton.setTitleColor(UIColor(red: 1, green: 1, blue: 1,alpha:1), for: UIControl.State.normal)

        

        
        self.tileOne.numberOfLines = 0
        self.tileOne.textAlignment = NSTextAlignment.center
        self.addSubview(self.tileOne)

        
        self.tileTwo.numberOfLines = 0
        self.tileTwo.textAlignment = NSTextAlignment.center
        self.addSubview(self.tileTwo)
     
        self.addSubview(self.seeDetails)
        self.seeDetails.titleLabel?.font = UIFont(name: "PingFang SC", size: 17)
        self.seeDetails.backgroundColor = UIColor(red: 0.25, green: 0.59, blue: 0.92, alpha: 1)
        self.seeDetails.layer.cornerRadius = 4
        self.seeDetails.setTitle("查看详情", for: UIControl.State.normal)
        self.seeDetails.setTitleColor(UIColor(red: 1, green: 1, blue: 1,alpha:1), for: UIControl.State.normal)

        //关闭
        self.close.layer.cornerRadius = 2;
        self.addSubview(self.close)
        self.close.setImage(UIImage.init(named: "HTLive.bundle/examinatioCclose_slices"), for: UIControl.State.normal)
        self.close.backgroundColor = UIColor.clear
        self.close.addTarget(self, action: #selector(self.closeClick), for: UIControl.Event.touchUpInside)
        self.close.isHidden = true
    }
    @objc func closeClick(){
        
        UIView.animate(withDuration: 0.5, delay: 0.0, options: UIView.AnimationOptions.allowAnimatedContent) {
            self.isHidden = true
            self.removeFromSuperview()
        } completion: { Bool in
            
        }
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        let cancelButtonX = 27.5
        let cancelButtonW = 110.0
        let cancelButtonH = 40.0
        let cancelButtonY = self.frame.height - 25.0 - cancelButtonH
        self.cancelButton.frame = CGRect(x: cancelButtonX, y: cancelButtonY, width: cancelButtonW, height: cancelButtonH)
        
       
        let sureButtonW = cancelButtonW
        let sureButtonX = self.frame.width - cancelButtonX - cancelButtonW
        let sureButtonY = cancelButtonY
        let sureButtonH = cancelButtonH
        self.sureButton.frame = CGRect(x: sureButtonX, y: sureButtonY, width: sureButtonW, height: sureButtonH)
        
        let seeDetailsW = 235.0
        let seeDetailsH = 40.0
        let seeDetailsY = self.frame.height - seeDetailsH - 25.0
        let seeDetailsX = (self.frame.width - seeDetailsW)/2.0
        self.seeDetails.frame = CGRect(x: seeDetailsX, y: seeDetailsY, width: seeDetailsW, height: seeDetailsH)
        
        let closeY = 15.0
        let closeW = 15.0
        let closeX = self.frame.width - 15.0 - 15.0
        let closeH = 15.0
        
        self.close.frame = CGRect(x: closeX, y: closeY, width: closeW, height: closeH)
    }
    

    func attributedString(text:NSString,fontSize:CGFloat ,color:UIColor)-> NSMutableAttributedString
    {
        let attrString = NSMutableAttributedString(string:text as String)
        let attr: [NSAttributedString.Key : Any] = [.font: UIFont(name: "PingFang SC", size: fontSize) as Any,.foregroundColor: color, ]
        attrString.addAttributes(attr, range: NSRange(location: 0, length: attrString.length))
        return attrString
    }
    
    
    func oneLine(text1:NSMutableAttributedString,text2:NSMutableAttributedString)
    {

        self.tileOne.attributedText = text1
        self.tileOne.frame = CGRect(x: 0, y: 32, width: self.frame.width, height: 25)
        
    
        self.tileTwo.attributedText = text2
        self.tileTwo.frame = CGRect(x: 0, y: self.tileOne.frame.maxY + 5.0, width: self.frame.width, height: 25)
    }

    //确认或者
    @objc func sureButtonClick()
    {
        if (type == TalkfunAlertType.deleted)
        {
            
            UIView.animate(withDuration: 0.5, delay: 0.0, options: UIView.AnimationOptions.allowAnimatedContent) {
                self.isHidden = true
                self.removeFromSuperview()
            } completion: { Bool in
                
            }
        }
        
    }
   
    
    //黑色
    func blackColor()->UIColor{
        
        return UIColor(red: 0.08, green: 0.09, blue: 0.1,alpha:1)
    }
    
    //灰色
    func greyColor()->UIColor{
        
        return UIColor(red: 0.59, green: 0.63, blue: 0.67,alpha:1)
    }
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    @objc public static func shareInstance(type:TalkfunAlertType,target:NSObject  ,action:Selector ,ID :NSInteger) {
         
        let keyWindow = TalkfunSharedTools.keyWindow()
       
            let windowW = (keyWindow.frame.size.width) as CGFloat
            let windowH = (keyWindow.frame.size.height) as CGFloat
            
            let alertNewView = TalkfunAlertNewView.init()
            let alertNewViewW = 290.0
            let alertNewViewH = 175.0
            let alertNewViewX = (windowW - alertNewViewW)/2.0
            let alertNewViewY = (windowH - alertNewViewH)/2.0

           alertNewView.configuration(CGRect(x: alertNewViewX, y: alertNewViewY, width: alertNewViewW, height: alertNewViewH), type: type,action:action ,target:target ,ID:ID)
        
            keyWindow.addSubview(alertNewView)
        
        
       
     }
    func configuration( _ frame:CGRect,type:TalkfunAlertType ,action:Selector,target:NSObject ,ID :NSInteger)
    {
        self.cancelButton.addTarget(self, action: #selector(self.cancelButtonClick), for: UIControl.Event.touchUpInside)
        self.close.isHidden = true
        self.type = type
        self.frame = frame
        self.seeDetails.setTitle("查看详情", for: UIControl.State.normal)
        
        
        if (type == TalkfunAlertType.submitToFill)
        {
            let one = self.attributedString(text: "确定要提交吗？", fontSize: 18, color: self.blackColor())
            let two = self.attributedString(text: "提交后无法继续作填写", fontSize: 16, color: self.greyColor())
            self.oneLine(text1: one, text2:two)
            self.seeDetails.isHidden = true
            self.sureButton.addTarget(target, action: action, for: UIControl.Event.touchUpInside)
            self.close.isHidden = false
        }
        
       else if (type == TalkfunAlertType.submit)
        {
            let one = self.attributedString(text: "确定要提交吗？", fontSize: 18, color: self.blackColor())
            let two = self.attributedString(text: "提交后无法继续作答", fontSize: 16, color: self.greyColor())
            self.oneLine(text1: one, text2:two)
            self.seeDetails.isHidden = true
            self.sureButton.addTarget(target, action: action, for: UIControl.Event.touchUpInside)
            self.close.isHidden = false
        }
        else if (type == TalkfunAlertType.notCompletedButWantToSubmit)
        {
            let one = self.attributedString(text: "你还有题目未做完", fontSize: 18, color: self.blackColor())
            let two = self.attributedString(text: "确定要提交吗?", fontSize: 18, color: self.blackColor())
            self.oneLine(text1: one, text2:two)
            self.seeDetails.isHidden = true
            self.sureButton.addTarget(target, action: action, for: UIControl.Event.touchUpInside)
            
            
        }
        else if (type == TalkfunAlertType.endAnswerAndTheStudentHasSubmitted)
        {
            let text1 = self.attributedString(text: "讲师已结束作答", fontSize: 18, color: self.blackColor())
            self.tileOne.attributedText = text1
            self.tileOne.frame = CGRect(x: 0, y: 45, width: self.frame.width, height: 25)
            
            let text2 = self.attributedString(text: "", fontSize: 18, color: self.greyColor())
            self.tileTwo.attributedText = text2
            self.tileTwo.frame = CGRect(x: 0, y: self.tileOne.frame.maxY + 5.0, width: self.frame.width, height: 25)
//            let one = self.attributedString(text: "讲师已结束作答", fontSize: 18, color: self.blackColor())
//            //本次测试已答对5道题
//            let two = self.attributedString(text: "", fontSize: 16, color:self.greyColor())
//            self.oneLine(text1: one, text2:two)
            self.seeDetails.isHidden = false
            self.seeDetails.addTarget(target, action: action, for: UIControl.Event.touchUpInside)
            self.seeDetails.tag = ID
            self.close.isHidden = false
        }
        else if (type == TalkfunAlertType.endOfExamTimeAndTheStudentHasSubmitted)
        {
            //本次测试已答对5道题
//            let one = self.attributedString(text: "答题时间到", fontSize: 18, color: self.blackColor())
//            let two = self.attributedString(text: "", fontSize: 18, color: self.greyColor())
//            self.oneLine(text1: one, text2:two)
            let text1 = self.attributedString(text: "答题时间到", fontSize: 18, color: self.blackColor())
            self.tileOne.attributedText = text1
            self.tileOne.frame = CGRect(x: 0, y: 45, width: self.frame.width, height: 25)
            
            let text2 = self.attributedString(text: "", fontSize: 18, color: self.greyColor())
            self.tileTwo.attributedText = text2
            self.tileTwo.frame = CGRect(x: 0, y: self.tileOne.frame.maxY + 5.0, width: self.frame.width, height: 25)
            self.seeDetails.isHidden = false
            self.seeDetails.addTarget(target, action: action, for: UIControl.Event.touchUpInside)
            self.seeDetails.tag = ID
            self.close.isHidden = false
        }
        else if (type == TalkfunAlertType.questionnaireDeleted)
        {
            
            let text1 = self.attributedString(text: "该表单已被删除", fontSize: 18, color: self.blackColor())
            self.tileOne.attributedText = text1
            self.tileOne.frame = CGRect(x: 0, y: 45, width: self.frame.width, height: 25)
            
            let text2 = self.attributedString(text: "", fontSize: 18, color: self.greyColor())
            self.tileTwo.attributedText = text2
            self.tileTwo.frame = CGRect(x: 0, y: self.tileOne.frame.maxY + 5.0, width: self.frame.width, height: 25)
            
            
            self.seeDetails.isHidden = false
            self.seeDetails.setTitle("知道了", for: UIControl.State.normal)
            self.seeDetails.addTarget(target, action: action, for: UIControl.Event.touchUpInside)
            
            self.close.isHidden = false
        }
        else if (type == TalkfunAlertType.endTheQuestionnaireEarly)
        {
            let one = self.attributedString(text: "讲师已回收表单", fontSize: 18, color: self.blackColor())
            //，\n本次测试已答对5道题
            let two = self.attributedString(text: "填写的表单内容已自动提交", fontSize: 18, color:self.greyColor())

            self.oneLine(text1: one, text2:two)
            self.seeDetails.isHidden = false
            self.seeDetails.addTarget(target, action: action, for: UIControl.Event.touchUpInside)
            self.seeDetails.tag = ID
            self.close.isHidden = false
        }
        else if (type == TalkfunAlertType.missingRequiredQuestions)
        {
         
            let one = self.attributedString(text: "当前有未填写的题目，", fontSize: 18, color: self.blackColor())
            let two = self.attributedString(text: "请填写完整！", fontSize: 18, color: self.blackColor())
            self.oneLine(text1: one, text2:two)
            self.seeDetails.isHidden = true
            self.sureButton.addTarget(target, action: action, for: UIControl.Event.touchUpInside)
          
        }
        
        else if (type == TalkfunAlertType.deleted)
        {
            
            let text1 = self.attributedString(text: "该测试已被删除", fontSize: 18, color: self.blackColor())
            self.tileOne.attributedText = text1
            self.tileOne.frame = CGRect(x: 0, y: 45, width: self.frame.width, height: 25)
            
            let text2 = self.attributedString(text: "", fontSize: 18, color: self.greyColor())
            self.tileTwo.attributedText = text2
            self.tileTwo.frame = CGRect(x: 0, y: self.tileOne.frame.maxY + 5.0, width: self.frame.width, height: 25)
            
            
            self.seeDetails.isHidden = false
            self.seeDetails.setTitle("知道了", for: UIControl.State.normal)
            self.seeDetails.addTarget(target, action: action, for: UIControl.Event.touchUpInside)
        }
        else if (type == TalkfunAlertType.otherTestQuestionsEndedAndTheStudentHasSubmitted)
        {
            

            let text1 = self.attributedString(text: "另一个测试答题时间到", fontSize: 18, color: self.blackColor())
            self.tileOne.attributedText = text1
            self.tileOne.frame = CGRect(x: 0, y: 45, width: self.frame.width, height: 25)
            
            let text2 = self.attributedString(text: "", fontSize: 18, color: self.greyColor())
            self.tileTwo.attributedText = text2
            self.tileTwo.frame = CGRect(x: 0, y: self.tileOne.frame.maxY + 5.0, width: self.frame.width, height: 25)

            self.seeDetails.isHidden = false
            self.seeDetails.addTarget(target, action: action, for: UIControl.Event.touchUpInside)
            self.seeDetails.tag = ID
            self.close.isHidden = false
            
            self.understand()
            
            
        }else if (type == TalkfunAlertType.otherTestQuestionsEndedEarlyAndTheStudentHasSubmitted)
        {
            

            let text1 = self.attributedString(text: "另一个测试讲师已结束作答", fontSize: 18, color: self.blackColor())
            self.tileOne.attributedText = text1
            self.tileOne.frame = CGRect(x: 0, y: 45, width: self.frame.width, height: 25)
            
            let text2 = self.attributedString(text: "", fontSize: 18, color: self.greyColor())
            self.tileTwo.attributedText = text2
            self.tileTwo.frame = CGRect(x: 0, y: self.tileOne.frame.maxY + 5.0, width: self.frame.width, height: 25)

            self.seeDetails.isHidden = false
            self.seeDetails.addTarget(target, action: action, for: UIControl.Event.touchUpInside)
            self.seeDetails.tag = ID
            self.close.isHidden = false
    
            self.understand()
            
        }else if (type == TalkfunAlertType.endAnswer)
        {
            let one = self.attributedString(text: "讲师已结束作答", fontSize: 18, color: self.blackColor())
            //，\n本次测试已答对5道题
            let two = self.attributedString(text: "试题已自动提交,正在努力批改中", fontSize: 18, color:self.greyColor())

            self.oneLine(text1: one, text2:two)
            self.seeDetails.isHidden = false
            self.seeDetails.addTarget(target, action: action, for: UIControl.Event.touchUpInside)
            self.seeDetails.tag = ID
            self.close.isHidden = false
        }
        else if (type == TalkfunAlertType.endOfExamTime)
        {
            let one = self.attributedString(text: "答题时间到", fontSize: 18, color: self.blackColor())
            // \n本次测试已答对20道题
            let two = self.attributedString(text: "试题已自动提交,正在努力批改中", fontSize: 18, color:self.greyColor())


            self.oneLine(text1: one, text2:two)
            self.seeDetails.isHidden = false
            self.seeDetails.addTarget(target, action: action, for: UIControl.Event.touchUpInside)
            self.seeDetails.tag = ID
            self.close.isHidden = false
        }else if (type == TalkfunAlertType.otherTestQuestionsEndedEarly)
        {
            
            let one = self.attributedString(text: "另一个测试讲师已结束作答", fontSize: 18, color: self.blackColor())
            //，已答对12道题
            let two = self.attributedString(text: "试题已自动提交,正在努力批改中", fontSize: 18, color: self.greyColor())
            self.oneLine(text1: one, text2:two)
            self.seeDetails.isHidden = false
            self.seeDetails.addTarget(target, action: action, for: UIControl.Event.touchUpInside)
            self.seeDetails.tag = ID
            self.close.isHidden = false
            self.understand()
        }else if (type == TalkfunAlertType.otherTestQuestionsEnded)
        {
           
            let one = self.attributedString(text: "另一个测试答题时间到", fontSize: 18, color: self.blackColor())
            //，已答对12道题
            let two = self.attributedString(text: "试题已自动提交,正在努力批改中", fontSize: 18, color: self.greyColor())
            self.oneLine(text1: one, text2:two)
            self.seeDetails.isHidden = false
            self.seeDetails.addTarget(target, action: action, for: UIControl.Event.touchUpInside)
            self.seeDetails.tag = ID
            self.close.isHidden = false
            self.understand()
        }
        
        
        if (self.seeDetails.isHidden)
        {
            self.sureButton.isHidden = false
            self.cancelButton.isHidden = false
        }else{
            self.sureButton.isHidden = true
            self.cancelButton.isHidden = true
        }
    }
    
    func understand()
    {
        self.seeDetails.setTitle("知道了", for: UIControl.State.normal)
        self.seeDetails.setTitleColor(UIColor(red: 0.2, green: 0.2, blue: 0.2,alpha:1), for: UIControl.State.normal)
        self.seeDetails.backgroundColor = UIColor(red: 0.96, green: 0.96, blue: 0.98, alpha: 1)
    }
    //取消
    @objc func cancelButtonClick(btn:UIButton) {
        
        UIView.animate(withDuration: 0.5, delay: 0.0, options: UIView.AnimationOptions.allowAnimatedContent) {
            btn.superview?.removeFromSuperview()
        } completion: { Bool in
            
        }
    }
    
}
